package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.AdMaterialCommonBean;
import com.hq.fxh.Modle.GetMaterialByCategoryIdRes;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;

public class AdCommonMaterialsFragment extends BaseFragment {

    private SwipeRefreshLayout refresh;
    private RecyclerView recyclerView;
    private int categoryId;
    private int PageCurrent =1;
    private int pageSize = 10;
    private List<AdMaterialCommonBean> adDatas = new ArrayList<>();
    private int uid;
    private AdMaterialAdapter adapter;
    private String titleName;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_ad_common_materials;
    }

    @Override
    protected void findView(View view) {
        refresh = (SwipeRefreshLayout) view.findViewById(R.id.refresh);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    }

    @Override
    protected void initData() {
        uid = SharedPreferenceUtils.getMemberId(getActivity());
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        categoryId = (int) getArguments().get("material_id");
        netRequest(categoryId);


      /*  refresh.setColorSchemeColors(getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimary));
        refresh.setDirection(SwipeRefreshLayoutDirection.BOTH);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipeRefreshLayoutDirection direction) {
                if (direction == SwipeRefreshLayoutDirection.TOP) {//刷新
                    PageCurrent = 1;
                    netRequest(categoryId);
                } else {//加载更多
                    PageCurrent++;
                    netRequest(categoryId);
                }
            }
        });*/
    }

    private void netRequest(int categoryId) {
        UserServiceImpl.getMaterialByCategoryId(categoryId, PageCurrent, pageSize, new NetworkSubscriber<GetMaterialByCategoryIdRes>() {
            @Override
            public int onSuccess(GetMaterialByCategoryIdRes data) {
                super.onSuccess(data);
                refresh.setRefreshing(false);
                if (data != null && data.getCode() == 0) {
                    if (PageCurrent == 1) {
                        adDatas.clear();
                    }
                    adDatas.addAll(data.getData());
                    if (adapter == null) {
                        adapter = new AdMaterialAdapter(getActivity(), adDatas, uid,false);
                        recyclerView.setAdapter(adapter);
                    }
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.show(getActivity(), data.getMsg());
                }
                return 0;
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        titleName = (String) getArguments().get("titleName");
        if (isVisibleToUser) {
            JAnalyticsInterface.onPageStart(getContext(), "通用素材-" + titleName);
        } else {
            JAnalyticsInterface.onPageEnd(getContext(),"通用素材-" + titleName);
        }
    }
}
